<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #entitySlot="{ model, field }">
        <a-input-search :disabled="title === '详情'" v-model:value="model[field]" placeholder="请选择实体名称" @search="entityOpen" />
      </template>
      <template #customSlot="{ model, field }">
        <a-input-search :disabled="title === '详情'" v-model:value="model[field]" placeholder="请选择编码规则名称" @search="open" />
      </template>
    </BasicForm>
  </BasicModal>
  <!-- 表单区域 -->
  <RuleSelectModal @register="regModal" @getSelectResult="handleSuccess" v-bind="getBindValue"></RuleSelectModal>
  <!-- 表单区域 -->
  <EntitySelectModal @register="regEntityModal" @getSelectResult="handleEntitySuccess" v-bind="getBindValue"></EntitySelectModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { useModal } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from '../BasDocumentType.data';
import { saveOrUpdate } from '../BasDocumentType.api';
import RuleSelectModal from './RuleSelectModal.vue';
import EntitySelectModal from './EntitySelectModal.vue';

// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const staus = ref<string>()
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 120,
  schemas: formSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 8 },
});
const [regModal, { openModal }] = useModal();
const [regEntityModal, { openModal: entityOpenModal }] = useModal();

function open() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}

//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  staus.value = data.status
  if (unref(isUpdate)) {
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  }
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !data?.showFooter });
});
const getBindValue = Object.assign({}, '');
//打开实体
function entityOpen() {
  entityOpenModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
//选择回写
function handleSuccess(val) {
  setFieldsValue({
    codingRuleName: val[0].label,
    codingRuleId: val[0].value,
  });
}
//选择回写
function handleEntitySuccess(val) {
  console.log(val)
  setFieldsValue({
    entityName: val[0].label,
    entityId: val[0].value,
    tableName:val[0].item.tableName
  });
}
//设置标题
const title = computed(() => staus.value);
// const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
//表单提交事件
async function handleSubmit(v) {
  try {
    let values = await validate();

    setModalProps({ confirmLoading: true });
    //提交表单
    await saveOrUpdate(values, isUpdate.value);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
  width: 100%;
}

:deep(.ant-calendar-picker) {
  width: 100%;
}
</style>